package com.hwpt.chesedemo.presentation.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowForwardIos
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hwpt.chesedemo.presentation.component.Difficulty
import com.hwpt.chesedemo.presentation.theme.AppStrings
import com.hwpt.chesedemo.presentation.theme.GradientEnd
import com.hwpt.chesedemo.presentation.theme.GradientStart
import com.hwpt.chesedemo.presentation.theme.strings

@Composable
fun DifficultySelectionScreen(
    onDifficultySelected: (Difficulty) -> Unit,
    modifier: Modifier = Modifier
) {
    val strings = strings()
    val difficulties = AppStrings.getDifficulties()

    // 检测屏幕尺寸，适配Pad
    val configuration = LocalConfiguration.current
    val screenWidth = configuration.screenWidthDp.dp
    val isTablet = screenWidth >= 600.dp
    val screenHeight = configuration.screenHeightDp.dp

    Column(
        modifier = modifier
            .fillMaxSize()
            .background(
                brush = Brush.verticalGradient(
                    colors = listOf(GradientStart, GradientEnd)
                )
            )
            .padding(if (isTablet) 32.dp else 16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        // 标题区域 - 手机端优化
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(if (isTablet) 16.dp else 8.dp),
            modifier = Modifier.weight(if (isTablet) 0.2f else 0.25f)
        ) {
            Text(
                text = strings.selectAiDifficulty,
                fontSize = if (isTablet) 48.sp else 28.sp, // 手机端减小字体
                fontWeight = FontWeight.ExtraBold,
                color = Color.White,
                textAlign = TextAlign.Center,
                letterSpacing = 0.5.sp
            )

            Text(
                text = strings.chooseOpponentByLevel,
                fontSize = if (isTablet) 20.sp else 14.sp, // 手机端减小字体
                color = Color.White.copy(alpha = 0.9f),
                textAlign = TextAlign.Center,
                lineHeight = if (isTablet) 24.sp else 16.sp
            )
        }

        // 难度选择区域 - 根据屏幕尺寸调整布局
        if (isTablet) {
            // Pad端：使用网格布局，每行显示2个
            val chunkedDifficulties = difficulties.chunked(2)
            Column(
                modifier = Modifier
                    .fillMaxWidth(0.9f)
                    .weight(0.6f),
                verticalArrangement = Arrangement.spacedBy(20.dp)
            ) {
                chunkedDifficulties.forEach { rowDifficulties ->
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.spacedBy(20.dp)
                    ) {
                        rowDifficulties.forEach { difficulty ->
                            DifficultyItem(
                                difficulty = difficulty,
                                onClick = { onDifficultySelected(difficulty) },
                                modifier = Modifier
                                    .weight(1f)
                                    .height(140.dp) // 减小高度
                            )
                        }
                        if (rowDifficulties.size == 1) {
                            Spacer(modifier = Modifier.weight(1f))
                        }
                    }
                }
            }
        } else {
            // 手机端：紧凑的垂直列表布局
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(0.6f),
                verticalArrangement = Arrangement.spacedBy(12.dp) // 减小间距
            ) {
                difficulties.forEachIndexed { index, difficulty ->
                    DifficultyItem(
                        difficulty = difficulty,
                        onClick = { onDifficultySelected(difficulty) },
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(80.dp), // 手机端进一步减小高度
                        isTablet = isTablet,
                        index = index
                    )
                }
            }
        }

        // 底部提示区域
        Column(
            modifier = Modifier.weight(if (isTablet) 0.2f else 0.15f),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Bottom
        ) {
            if (isTablet) {
                Text(
                    text = strings.tapToStartGame,
                    fontSize = 18.sp,
                    color = Color.White.copy(alpha = 0.7f),
                    fontStyle = FontStyle.Italic
                )
            } else {
                Text(
                    text = strings.tapToSelectDifficulty,
                    fontSize = 12.sp,
                    color = Color.White.copy(alpha = 0.6f),
                    fontStyle = FontStyle.Italic
                )
            }
        }
    }
}

@Composable
fun DifficultyItem(
    difficulty: Difficulty,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    isTablet: Boolean = false,
    index: Int = 0
) {
    val strings = strings()
    val interactionSource = remember { MutableInteractionSource() }

    // 获取实际的多语言文本
    val difficultyName = AppStrings.getDifficultyName(strings, difficulty.nameKey)
    val difficultyDescription = AppStrings.getDifficultyDescription(strings, difficulty.descriptionKey)
    val difficultyIcon = AppStrings.getDifficultyIcon(strings, difficulty.iconKey)

    // 为手机端添加不同的背景颜色渐变
    val phoneGradientColors = listOf(
        Color.White.copy(alpha = 0.12f),
        Color.White.copy(alpha = 0.18f)
    )

    val tabletGradientColors = listOf(
        Color.White.copy(alpha = 0.15f),
        Color.White.copy(alpha = 0.25f)
    )

    Box(
        modifier = modifier
            .shadow(
                elevation = if (isTablet) 6.dp else 4.dp,
                shape = RoundedCornerShape(if (isTablet) 16.dp else 12.dp),
                clip = true
            )
            .clip(RoundedCornerShape(if (isTablet) 16.dp else 12.dp))
            .background(
                brush = Brush.verticalGradient(
                    colors = if (isTablet) tabletGradientColors else phoneGradientColors
                )
            )
            .border(
                width = if (isTablet) 1.5.dp else 1.dp,
                color = Color.White.copy(alpha = if (isTablet) 0.2f else 0.15f),
                shape = RoundedCornerShape(if (isTablet) 16.dp else 12.dp)
            )
            .clickable(
                interactionSource = interactionSource,
                indication = null
            ) { onClick() }
            .padding(if (isTablet) 20.dp else 16.dp), // 减小内边距
        contentAlignment = Alignment.Center
    ) {
        if (isTablet) {
            // Pad端布局 - 紧凑版本
            Row(
                modifier = Modifier.fillMaxSize(),
                horizontalArrangement = Arrangement.spacedBy(16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Box(
                    modifier = Modifier
                        .size(60.dp) // 减小图标大小
                        .background(
                            color = Color.White.copy(alpha = 0.1f),
                            shape = CircleShape
                        ),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = difficultyIcon,
                        fontSize = 28.sp, // 减小字体
                        modifier = Modifier.padding(6.dp)
                    )
                }

                Column(
                    modifier = Modifier.weight(1f),
                    horizontalAlignment = Alignment.Start,
                    verticalArrangement = Arrangement.spacedBy(6.dp) // 减小间距
                ) {
                    Text(
                        text = difficultyName,
                        fontSize = 20.sp, // 减小字体
                        fontWeight = FontWeight.Bold,
                        color = Color.White
                    )
                    Text(
                        text = difficultyDescription,
                        fontSize = 14.sp, // 减小字体
                        color = Color.White.copy(alpha = 0.9f),
                        lineHeight = 16.sp
                    )
                }

                Icon(
                    imageVector = Icons.Default.ArrowForwardIos,
                    contentDescription = strings.select,
                    tint = Color.White.copy(alpha = 0.7f),
                    modifier = Modifier.size(20.dp) // 减小箭头
                )
            }
        } else {
            // 手机端：紧凑水平布局
            Row(
                modifier = Modifier.fillMaxSize(),
                horizontalArrangement = Arrangement.spacedBy(12.dp), // 减小间距
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 图标区域 - 更小
                Box(
                    modifier = Modifier
                        .size(48.dp) // 减小图标大小
                        .background(
                            brush = Brush.radialGradient(
                                colors = listOf(
                                    Color.White.copy(alpha = 0.2f),
                                    Color.White.copy(alpha = 0.1f)
                                )
                            ),
                            shape = CircleShape
                        )
                        .border(
                            width = 1.dp,
                            color = Color.White.copy(alpha = 0.3f),
                            shape = CircleShape
                        ),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = difficultyIcon,
                        fontSize = 20.sp, // 减小字体
                        color = Color.White
                    )
                }

                // 文本信息区域
                Column(
                    modifier = Modifier.weight(1f),
                    horizontalAlignment = Alignment.Start,
                    verticalArrangement = Arrangement.spacedBy(4.dp) // 减小间距
                ) {
                    Text(
                        text = difficultyName,
                        fontSize = 16.sp, // 减小字体
                        fontWeight = FontWeight.Bold,
                        color = Color.White,
                        letterSpacing = 0.2.sp
                    )
                    Text(
                        text = difficultyDescription,
                        fontSize = 12.sp, // 减小字体
                        color = Color.White.copy(alpha = 0.85f),
                        lineHeight = 14.sp,
                        maxLines = 2,
                        overflow = TextOverflow.Ellipsis
                    )
                }

                // 手机端使用更小的箭头
                Icon(
                    imageVector = Icons.Default.ArrowForwardIos,
                    contentDescription = strings.select,
                    tint = Color.White.copy(alpha = 0.6f),
                    modifier = Modifier
                        .size(16.dp) // 减小箭头
                        .padding(end = 2.dp)
                )
            }
        }
    }
}